<!-- 职级编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'新增明细':'新增明细'"
    :visible="visible"
    width="70%"
    :destroy-on-close="true"
    :lock-scroll="false"
    @open="init"
    @update:visible="updateVisible">
    <el-form
      :model="form"
      ref="form"
      label-width="77px"
      :rules="rules"
      class="ele-form-search">
      <el-row :gutter="15" style="padding-bottom: 55px;" >

        <el-col :lg="6" :md="6">
          <el-form-item label="库存数量:" label-width="100px"  prop="stockQty">
            <el-input
              clearable
              v-model="form.stockQty"
              placeholder=""/>
          </el-form-item>
        </el-col>

        <el-col :lg="6" :md="6">

          <el-form-item label="制单时间:" label-width="100px" prop="billtime">

            <el-date-picker v-model="form.billtime" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :lg="6" :md="6">

          <el-form-item label="控价开始时间:" label-width="120px" prop="controlBeginTime">

            <el-date-picker v-model="form.controlBeginTime" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :lg="6" :md="6">
          <el-form-item label="控价结束时间:" label-width="120px" prop="controlEndTime">

            <el-date-picker v-model="form.controlEndTime" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
          </el-form-item>
        </el-col>
        <br><br><br>
        <el-col :lg="6" :md="6">
          <el-form-item label="NC转换规则:" label-width="120px" prop="convertRate">
            <el-input
              clearable
              v-model="form.convertRule"
              placeholder=""/>
          </el-form-item>
        </el-col>

        <el-col :lg="6" :md="6">
          <el-form-item label="NC转换系数:" label-width="120px" prop="convertRate">
            <el-input
              clearable
              v-model="form.convertRate"
              placeholder=""/>
          </el-form-item>
        </el-col>

        <el-col :lg="6" :md="6">
          <el-form-item label="控价价格:" label-width="100px" prop="controlPrice">
            <el-input
              clearable
              v-model="form.controlPrice"
              placeholder=""/>
          </el-form-item>
        </el-col>

        <el-col :lg="6" :md="6">
          <el-form-item label="转化后价格:" label-width="100px" prop="convertedPrice">
            <el-input
              clearable
              v-model="form.convertedPrice"
              placeholder=""/>
          </el-form-item>
        </el-col>
        <br><br><br>
        <el-col :lg="12" :md="12">
          <el-form-item label="价格依据:" >
            <a v-bind:href="baseURL+'uploads/'+form.uploadfile" target="_blank" style="text-decoration: underline;">
              {{form.uploadfile?String(form.uploadfile).slice(String(form.uploadfile).indexOf('_')+1):''}}
            </a>
            <el-upload
              class="ele-block"
              action=""
              v-loading="uploadLoading"
              accept=".pdf,.docx,.doc"
              :show-file-list="false"
              :before-upload="beforeUpload">
              <el-button type="primary" icon="el-icon-upload" class="ele-btn-icon" size="small">点击上传
              </el-button>
            </el-upload>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>


    <div slot="footer">
      <el-button @click="updateVisible(false)">关闭</el-button>
      <el-button
                 type="primary"
                 @click="save"
      >保存
      </el-button>
    </div>
    <UploadFile :visible.sync="showUpload" @done="setUploadFile"></UploadFile>
  </el-dialog>
</template>

<script>
import UploadFile from '../../spbc/bctb/upload';
import setting from "@/config/setting";

export default {
  name: 'frmInvDocList',
  components:{UploadFile},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      //form: Object.assign({status: 1}, this.dataItem),
      url: '/invpricedetail/index',
      dataList:[],
      selection:[],
      form:{},
      baseURL:setting.baseURL,
      // 表格搜索条件


      // 表单验证规则
      rules: {
        stockQty: [
          {required: true, message: '请输入库存数量', trigger: 'blur'}
        ],
        billtime: [
          {required: true, message: '请选择制单时间', trigger: 'blur'}
        ],
        controlBeginTime: [
          {required: true, message: '请输入控价开始时间', trigger: 'blur'}
        ],
        controlEndTime: [
          {required: true, message: '请输入控价结束时间', trigger: 'blur'}
        ],
        convertRule: [
          {required: true, message: '请输入NC转换规则', trigger: 'blur'}
        ],
        convertRate: [
          {required: true, message: '请输入NC转换系数', trigger: 'blur'}
        ],
        controlPrice: [
          {required: true, message: '请输入控价价格', trigger: 'blur'}
        ],
        convertedPrice: [
          {required: true, message: '请输入转化后价格', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      showUpload: false,
      uploadLoading: false,
    };
  },
  watch: {
    data() {
      console.log(this.data)
      if (this.data) {
        this.where = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.where = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /*初始化表单数据*/
    init(){
      this.form={};
    },
    /* 刷新表格 */
    reload() {
      //console.log('wwww');
      this.$refs.table.reload();
    },
    openUpload(){
      this.showUpload = true;
    },
    setUploadFile(filename)
    {
      this.$set(this.form,'uploadfile',filename);
    },
    /* 上传 */
    beforeUpload(file) {
      this.uploadLoading = true;
      let formData = new FormData();
      formData.append('file', file);
      formData.append('dir','invprice')
      this.$http.post('/invprice/uploadfile', formData).then(res => {
        this.uploadLoading = false;
        if (res.data.code === 0) {
          this.$message({type: 'success', message: '附件上传成功'});
          this.form.priceAttach=res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.loading = false;
        this.$uploadLoading.error(e.message);
      });
      return false;
    },
    querydata()
    {
      if(!this.where.invcode && !this.where.invname)
      {
        this.$message.info("请药品编码或药品名称后查询。");
        return;
      }
      var url = '/ygorder/ncinvlist?ts=1';
      if(this.where.invname)
      {
        url+="&invname="+this.where.invname;
      }
      if(this.where.invcode)
      {
        url+="&invcode="+this.where.invcode;
      }
      this.$http.get(url).then(res=>{
        if (res.data.code === 0) {
          this.dataList = res.data.data;
        }
        else
        {
          this.$message.error(res.data.msg);
        }

      });

    },
    loaddata(){
      var url = '/invprice/invlist?ts=1';
      if(this.where.invcode)
      {
        url+="&invcode="+this.where.invcode;
      }
      else {
        this.$message.info("请输入药品编码或药品名称后查询");
        return;
      }
      this.$http.get(url).then(res=>{
        if (res.data.code === 0) {
          this.dataList = res.data.data;
        }
        else
        {
          this.$message.error(res.data.msg);
        }

      });
    },
    handleRowClick(row)
    {
      if(this.selection.length > 0){
        let del_row = this.selection.shift()
        this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
      }
      this.selection.push(row);
      this.$refs.table.toggleRowSelection(row,true)
    },
    // select()
    // {
    //   if(this.form.length==0)
    //   {
    //     this.$message.info("请选择对应规格的药品编码。");
    //     return;
    //   }
    //   this.$emit('update:visible', false);
    //   this.$emit('done',this.selection[0].zxspbm,this.selection[0].cpm);
    //
    // },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        console.log(this.data);
        if (valid) {
          this.form.pid=this.data.id
          this.loading = true;
          this.$http[this.isUpdate ? 'put' : 'post'](this.isUpdate ? '/invpricedetail/edit' : '/invpricedetail/add', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
